package com.umeox.watch.moto.pedometer.base;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;

import com.dbflow5.database.FlowCursor;
import com.dbflow5.query.From;
import com.dbflow5.query.SQLite;
import com.dbflow5.query.Where;
import com.umeox.moto.common.log.Logger;
import com.umeox.moto.common.utils.DateUtils;


public class PedometerDBHelper {
    private static PedometerDBHelper instance;

    private PedometerDBHelper() {
    }

    public static synchronized PedometerDBHelper getInstance() {
        if (instance == null) {
            synchronized (PedometerDBHelper.class) {
                if (instance == null) {
                    instance = new PedometerDBHelper();
                }
            }
        }
        return instance;
    }


    public synchronized void insertOrUpdateStep(int stepCounts) {
        insertOrUpdateStep(DateUtils.getTodayMillis(), stepCounts);
    }

    public synchronized void insertOrUpdateStep(int saveTimeMillis, int stepCount) {
        AsyncTask.execute(new Runnable() {
            @Override
            public void run() {
                final ContentValues contentValues = new ContentValues();
                contentValues.put("stepTime", saveTimeMillis);
                contentValues.put("stepValue", stepCount);
                BaseDatabase.getDatabase()
                        .getWritableDatabase()
                        .insertWithOnConflict(StepInfo.TABLE_NAME, null, contentValues, SQLiteDatabase.CONFLICT_REPLACE);
            }
        });
    }

    public  int getStepCount() {
        int stepsToday = 0;
        FlowCursor cursor = null;
        try {
            cursor = BaseDatabase
                    .getDatabase()
                    .getWritableDatabase()
                    .query(StepInfo.TABLE_NAME, null, "stepTime = ?", new String[]{String.valueOf(DateUtils.getTodayMillis())}, null, null, null);
            if (cursor.moveToFirst()) {
                stepsToday = cursor.getInt(cursor.getColumnIndex("stepValue"));
            }
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        return stepsToday;
    }


    /**
     * Removes invalid entries from the database.
     * <p/>
     * Currently, an invalid input is such with steps >= 200,000
     */
    public void removeInvalidEntries() {
        Where remove = SQLite.delete().from(StepInfo.class).where(StepInfo_Table.stepValue.greaterThanOrEq(200000));
        long affected = remove.executeUpdateDelete(BaseDatabase.getDatabase());
        Logger.d("removeInvalidEntries->sql->" + remove.getQuery() + "->affected->" + affected);
    }

    public void removeALlEntries() {
        From remove = SQLite.delete().from(StepInfo.class);
        long affected = remove.executeUpdateDelete(BaseDatabase.getDatabase());
        Logger.d("removeALlEntries->sql->" + remove.getQuery() + "->affected->" + affected);

    }


}
